import type { EpPropMergeType } from 'element-plus/es/utils/index.mjs'
import { defineStore } from 'pinia'
import { reactive, ref } from 'vue'

interface ElMessageItem {
  message: string
  type:
    | EpPropMergeType<StringConstructor, 'success' | 'warning' | 'error' | 'info', unknown>
    | undefined
}

export function ElMessage({
  type,
  message
}: {
  type: 'success' | 'warning' | 'error' | 'info'
  message: string
}) {
  const elMessage = useElMessage()
  elMessage.setElMessage(message, type)
}
export const useElMessage = defineStore('elMessage', () => {
  const elMessageNum = ref<number>(0)

  const elMessages = reactive<ElMessageItem[]>([])

  // 设置消息提示
  const setElMessage = (
    message: string,
    type?:
      | EpPropMergeType<StringConstructor, 'success' | 'warning' | 'error' | 'info', unknown>
      | undefined
  ) => {
    elMessages.push({
      message,
      type
    })
    elMessageNum.value = elMessages.length
  }

  // 删除消息提示
  const deleteElMessages = () => {
    elMessages.length = 0
    elMessageNum.value = elMessages.length
  }
  return { setElMessage, elMessageNum, elMessages, deleteElMessages }
})
